/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.doctor.DoctorPatientGroup;

public interface DoctorPatientGroupDao extends PagingAndSortingRepository<DoctorPatientGroup, Long>, JpaSpecificationExecutor<DoctorPatientGroup> {

	@Query("select a from DoctorPatientGroup a where a.code = ?1 and a.status = 1")
	DoctorPatientGroup findByCode(String group);

	@Modifying
	@Query("update DoctorPatientGroup a set a.status = 0 where a.code = ?1")
	int deleteByCode(String group);
	
	// 查询医生的患者自定义分组
	@Query("select a from DoctorPatientGroup a where (a.doctor = ?1 or a.doctor is null) and a.status = 1 order by a.id")
	List<DoctorPatientGroup> findByDoctor(String doctor);
	
	// 根据患者信息标识获取患者所在分组
	@Query("SELECT pg FROM DoctorPatientGroup pg, DoctorPatientGroupInfo pgi WHERE pg.code = pgi.group AND pgi.doctor = ?1 and pgi.patient=?2 and pgi.status=1 and pgi.signType=2")
	DoctorPatientGroup findPatientGroupByPatient(String doctor, String patient);
}
